












#include <QtCore>
#include <QtNetwork>
#include <QtXml>

#include <kongm.h>
#include "kxfire.h"
#include "kxfiretest.h"


kXFire::kXFire(QObject *parent) :
    QObject(parent)
{
    QSettings settings( "GentooXativa" , "KON Game Manager" );
    test = new kXFireTest();

    test->loadGamesFromDefault();

    gamenames = test->getGameNames();

    if( settings.value("XFireDebug").toBool() == true )
    {
        test->show();
    }

    test->log( QString( "%1%2" ).arg( "Starting XFire module v").arg( XFIRE_MOD_VERSION ));
    test->log( QString( "by: %1 (%2)" ).arg( XFIRE_MOD_AUTHOR ).arg( XFIRE_MOD_EMAIL));

    lookForInstall();
}

/**
  * This function look for XFire executable and registry keys.
  *
  * @param path Force the path.
  */
bool kXFire::lookForInstall( QString *path )
{
    QString defPath( QString( "%1\\Xfire" ).arg( getenv( "PROGRAMFILES" )));

    QString defRegkey64( "HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Xfire" );
    QString defRegkey32( "HKEY_LOCAL_MACHINE\\SOFTWARE\\Xfire" );

    test->log( QString( "Default XFire path: %1" ).arg( defPath ));
    test->log( QString( "Looking in registry keys: %1" ).arg( defRegkey32 ));

    QSettings settings( defRegkey32, QSettings::NativeFormat );
    if( !settings.value("CommonDataPath").toString().isEmpty())
    {
        test->log( QString( "Founded, setting XFire path to: %1" ).arg( settings.value("CommonDataPath").toString()));
        path = new QString(settings.value("CommonDataPath").toString());
    }
    else
    {
        test->log( QString(  "Key not found in registry, trying 64-bits path: %1" ).arg( defRegkey64 ));
        QSettings settings64( defRegkey64, QSettings::NativeFormat );
        if( settings.contains("CommonDataPath"))
        {
            test->log( QString( "Founded, setting XFire path to: %1" ).arg( settings.value("CommonDataPath").toString()));
            path = new QString(settings.value("CommonDataPath").toString());
        }
        else
            return false;
    }

    QString txt("");
    txt.append( path );
    txt.append( "\\xfire.exe");
    test->log( QString( "Looking for xfire executable at %1" ).arg( txt ));

    QFile xfireExec( txt );

    if( xfireExec.exists() )
    {
        test->log( QString( "Founded XFire executable." ));
        return true;
    }
    else
    {
        test->log( QString( "XFire executable NOT found!" ));
    }

    return false;
}

bool kXFire::loadGames()
{

}

void kXFire::showDebugWindow( bool state )
{
    QSettings settings( "GentooXativa" , "KON Game Manager" );
    settings.setValue("XFireDebug", state );

    if( state == true )
    {
        if( test != 0 )
            test->show();
        else
        {
            test = new kXFireTest();
            test->show();
        }
    }
    else
    {
        test->hide();
    }
}
